﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WyUnity.Utility.ExtentionMethods
{
	/// <summary>
	/// 值类型扩展方法
	/// </summary>
	public static class ValueTypeExtensions
	{
		#region Functions

		#region ToBool

		/// <summary>
		/// Turns an int into a bool
		/// </summary>
		public static bool ToBool(this int input)
		{
			return input > 0;
		}

		#endregion

		#region ToInt

		/// <summary>
		/// Converts the bool to an integer
		/// </summary>
		public static int ToInt(this bool value)
		{
			return value ? 1 : 0;
		}

		#endregion

		#region ToBase64String

		/// <summary>
		/// Converts a byte array into a base 64 string
		/// </summary>
		/// <param name="input">Input array</param>
		/// <returns>The equivalent byte array in a base 64 string</returns>
		public static string ToBase64String(this byte[] input)
		{
			return input == null ? "" : Convert.ToBase64String(input);
		}

		#endregion

		#region ToEncodedString

		/// <summary>
		/// Converts a byte array to a string
		/// </summary>
		public static string ToEncodedString(this byte[] input, Encoding encodingUsing = null, int index = 0, int count = -1)
		{
			if (input == null)
				return "";
			if (count == -1)
				count = input.Length - index;
			return encodingUsing.NullCheck(new UTF8Encoding()).GetString(input, index, count);
		}

		#endregion

		#region IsUnicode

		/// <summary>
		/// Determines if a byte array is unicode
		/// </summary>
		public static bool IsUnicode(this byte[] input)
		{
			return input == null ? true : input.ToEncodedString(new UnicodeEncoding()).IsUnicode();
		}

		#endregion

		#endregion
	}
}
